package displacementMapFiler
{
	import flash.events.Event;
	import flash.display.StageScaleMode;
	import flash.display.StageAlign;
	import flash.filters.DisplacementMapFilter;
	import flash.geom.Matrix;
	import flash.display.GradientType;
	import utils.UtilsIsometric;
	import flash.geom.Point;
	import com.tweenman.Easing;
	import flash.display.BitmapData;
	import flash.display.Bitmap;
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.display.Sprite;

	public class Main extends Sprite
	{
		public var container:Sprite;
			public var gridContainer:Sprite;
				public var grid:Bitmap;
			public var planetsContainer:Sprite;
		
		public function Main()
		{
			addEventListener(Event.ENTER_FRAME, init);
		}
		private function init(e:Event):void
		{
			removeEventListener(Event.ENTER_FRAME, init);
			
			stage.align = StageAlign.TOP_LEFT;
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.stageFocusRect = false;
			
			setup();
			drawGrid();
			updateGravities();
			drawPlanets();
		}
		private function setup():void
		{
			container = addChild(new Sprite()) as Sprite;
				gridContainer = container.addChild(new Sprite()) as Sprite;
					grid = gridContainer.addChild(new Bitmap(new BitmapData(1, 1))) as Bitmap;
				planetsContainer = container.addChild(new Sprite()) as Sprite;
		}
		private function drawGrid():void
		{
			// размеры сетки
			var dx:int = 40;
			var dy:int = 20;
			
			var s:Shape = new Shape;
			var g:Graphics = s.graphics;
			
			g.lineStyle(1, 0);
			g.moveTo(0, dy);
			
			var maxX:int = container.stage.stageWidth / dx;
			var maxY:int = container.stage.stageHeight / dy;
			// рисуем сетку
			for(var y:int=0; y<maxY+maxX; y++)
			{
				g.moveTo(0, y*dy);
				g.lineTo(y*dx, 0);
				
				g.moveTo(container.stage.stageWidth, y*dy);
				g.lineTo(container.stage.stageWidth-y*dx, 0);
			}
			// копируем "рисунок" в битмап
			trace(container.stage.stageWidth, container.stage.stageHeight)
			
			var bitmapdata:BitmapData = new BitmapData(container.stage.stageWidth, container.stage.stageHeight, true, 0);
				bitmapdata.draw(s);
			// цепляем на экран
			grid.bitmapData.dispose();
			grid.bitmapData = bitmapdata; 
		}
		private function updateGravities():void
		{
			var funcs:Array = [Easing.easeOutQuart];//, Easing.easeOutCubic, Easing.easeOutCircular];
			
			var arr:Array = [];
			var params:DisplacementMapFilterGravityParams;
				params = new DisplacementMapFilterGravityParams();
				params.x = 250;
				params.y = 50;
				params.radius = 200;
				params.func = funcs[arr.length%funcs.length];
			arr.push( params );
			
				params = new DisplacementMapFilterGravityParams();
				params.x = 500;
				params.y = 100;
				params.radius = 300;
				params.func = funcs[arr.length%funcs.length];
			arr.push( params );
			
			
			var displacement:DisplacementMapFilterGravity = new DisplacementMapFilterGravity();
				displacement.arrGravities = arr;
				displacement.createDisplacementMap();
			var filter:DisplacementMapFilter = displacement.createDisplacementFilter();
			
			var s:Sprite = new Sprite();
			// копируется сетка, копия кладется в контейнер	
			var bitmap:Bitmap = s.addChild( new Bitmap(new BitmapData(grid.width, grid.height, true, 0xff0000)) ) as Bitmap;
				bitmap.bitmapData.draw(grid);
			
			s.filters = [filter];
					
			var bitmapData:BitmapData = new BitmapData(grid.width, grid.height, true, 0);
				bitmapData.draw(s);
					
			grid.bitmapData.dispose();
			grid.bitmapData = bitmapData;
		}
		private function drawPlanets():void
		{
			var p1:Point = new Point(UtilsIsometric.xToIsoX(230, 30), UtilsIsometric.yToIsoY(230, 30));
			var p2:Point = new Point(UtilsIsometric.xToIsoX(460, 70), UtilsIsometric.yToIsoY(460, 70));
			
			planetsContainer.graphics.clear();
			
			planetsContainer.graphics.lineStyle(2, 0x333333);
			
			planetsContainer.graphics.beginGradientFill(GradientType.RADIAL, [0xffffff,0], [1,1], [0, 0x66], new Matrix(1,0,0,.5,20,20));
			planetsContainer.graphics.drawCircle(p1.x, p1.y, 40);
			planetsContainer.graphics.endFill();
			
			planetsContainer.graphics.beginGradientFill(GradientType.RADIAL, [0xffffff,0], [1,1], [0, 0x66], new Matrix(1,0,0,.5,180,180));
			planetsContainer.graphics.drawCircle(p2.x, p2.y, 50);
			planetsContainer.graphics.endFill();
			
			
		}
	}
}
